package com.web.exiaodao.remote.controller;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.VSDCExperience;
import com.web.exiaodao.remote.service.ApiUserService;
import com.web.exiaodao.remote.service.ApiVSDCExperienceService;
import com.web.exiaodao.remote.vo.VSDCExperienceVo;
import com.web.webstart.base.util.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.XaUtil;
import com.web.exiaodao.remote.service.ApiVSDCService;
import com.web.exiaodao.remote.vo.VSDCVo;

/**
 * @Title: ApiVSDCController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 服务-义工中心信息接口
 * @author zhanglin
 * @date 2016-11-03 18:30:29
 * @version V1.0
 */
@Api(value = "ServerVSDC", description = "服务-义工中心接口", position = 10)
@Controller
@RequestMapping("/api/vSDC")
public class ApiServerVSDCController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(ApiServerVSDCController.class);

	@Autowired
	private ApiVSDCService vSDCService;
    @Autowired
    private ApiVSDCExperienceService vSDCExperienceService;
    @Autowired
    private ApiUserService userService;

	/**
	 * @Title: findVSDCList
	 * @Description: 查询所有服务-义工中心信息
	 * @return
	 */
	@ApiOperation(value="查询义工活动列表",notes="查询义工活动列表,当返回的code=1时，取出object进行显示,存放为数组对象")
	@ResponseBody
	@RequestMapping(value="findVSDCList",method=RequestMethod.POST)
	public XaResult<List<VSDCVo>> findVSDCList(
            @ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId", required = false) Long userId,
		@ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
		@ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
	) throws BusinessException{
		XaResult<List<VSDCVo>> xr = new XaResult<>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'sort',direction:'ASC'},{property:'clickCont',direction:'DESC'},{property:'createTime',direction:'DESC'}]");
			Map<String , Object> filterParams =  WebUitl.getParametersStartingWith("{}", "search_");
			xr =  vSDCService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
		}catch(Exception e){
			if (e instanceof BusinessException) {
				xr.error(e.getMessage());
			} else  {
				xr.error(XaConstant.Message.error);
			}
			LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}

	/**
	 * @Title: findVSDCById
	 * @Description: 根据ID查找单条实体
	 * //@param modelId
	 * @return    
	 */
	@ApiOperation(value="义工活动详情",notes="查询义工中心详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
	@ResponseBody
	@RequestMapping(value="findVSDCById",method=RequestMethod.POST)
	public XaResult<VSDCVo> findVSDCById(
		@ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id
	) throws BusinessException{
		XaResult<VSDCVo> xr = new XaResult<VSDCVo>();
		try{
			return vSDCService.findOne(id);
		}catch(Exception e){
			if (e instanceof BusinessException) {
						 xr.error(e.getMessage());
			} else  {
				 xr.error(XaConstant.Message.error);
			}
			 LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}

    /**
     * @return
     * @Title: save
     * @Description: 新增一条实体
     * //@param userId
     * //@param model
     */
    @ApiOperation(value = "加入义工活动", notes = "加入义工活动,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "joinVSDC", method = RequestMethod.POST)
    public XaResult<VSDCExperienceVo> joinVSDC(
            @ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId") Long userId,
            @ApiParam("义工活动id,字段名:vsdcId") @RequestParam(value = "vsdcId") Long vsdcId,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<VSDCExperienceVo> xr = new XaResult<VSDCExperienceVo>();
        if (XaUtil.isEmpty(userId)) {
            xr.error("用户Id不能为空！");
            return xr;
        }
        if (XaUtil.isEmpty(vsdcId)) {
            xr.error("义工活动id不能为空！");
            return xr;
        }
        try {
            //查询用户是否存在
            if (userId != null) {
                userService.findOne(userId);
            }
            VSDCExperience model = new VSDCExperience();
            model.setUserId(userId+"");
            model.setVSDC_Id(vsdcId);
            model.setVSDC_State(JConstant.VSDCStates.DEFAULT);
            model.setStatus(JConstant.VSDCStates.DEFAULT);
            model.setJoniDate(DateUtils.getToday(DateUtils.YYYYMMDDHHMMSS));
            return vSDCExperienceService.createModel(model);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage());
            } else  {
                xr.error(XaConstant.Message.error);
            }
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }

	/**
	 * @return
	 * @Title: findVSDCExperienceList
	 * @Description: 查询所有义工经历信息
	 */
	@ApiOperation(value = "查询自己的义工经历", notes = "查询自己的义工经历,当返回的code=1时，取出object进行显示,存放为数组对象")
	@ResponseBody
	@RequestMapping(value = "findVSDCExperienceList", method = RequestMethod.POST)
	public XaResult<List<VSDCExperienceVo>> findVSDCExperienceList(
			@ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId") Long userId,
			@ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
			@ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
	) throws BusinessException {
		XaResult<List<VSDCExperienceVo>> xr = new XaResult<List<VSDCExperienceVo>>();
		if (XaUtil.isEmpty(userId)) {
			xr.error("用户Id不能为空！");
			return xr;
		}
		if (XaUtil.isEmpty(userService.validUserId(userId+""))) {
			xr.error("用户Id不存在！");
			return xr;
		}
		try {
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
			Map<String, Object> filterParams = WebUitl.getParametersStartingWith(
			        "{'search_EQ_vSDC_State':'"+ JConstant.VSDCStates.PASS+"'" +
                    ",'search_EQ_userId':'"+userId+"'}", "search_");
			return vSDCExperienceService.findListNEStatusByFilter(XaConstant.Status.delete, filterParams, pageable);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				xr.error(e.getMessage());
			} else {
				xr.error(XaConstant.Message.error);
			}
		}
		return xr;
	}
}

